package org.grow.yc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.yc.dao.LeaveActionMapper;
import org.grow.yc.entity.LeaveAction;
import org.grow.yc.entity.LeaveBill;
import org.grow.yc.entity.dto.*;
import org.grow.yc.exception.BillStateException;
import org.grow.yc.exception.MissTargetException;
import org.grow.yc.filter.AuthInterceptor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 2024/9/30
 */

@RequestMapping("/leaveAction")
@RestController
public class LeaveActionController {

    @Autowired
    private LeaveActionMapper leaveActionMapper;

    //        这里的insert 指的是业务层面上，一个请假申请人刚刚新建一个请假单，还没提交给主管
    @PostMapping("/insert")
    public LeaveAction insert(@RequestBody LeaveInsertDto leaveInsertDto) {

        LeaveAction leaveAction = new LeaveAction();
//        ID最好有专用的ID生成器
        leaveAction.setLeaveId("LA" + System.currentTimeMillis());
        leaveAction.setActionState("toSubmit");

//        leaveAction.setStartTime(leaveInsertDto.getStartTime());
//        leaveAction.setEndTime(leaveInsertDto.getEndTime());
//        leaveAction.setPic(leaveInsertDto.getPic());
//
        BeanUtils.copyProperties(leaveInsertDto, leaveAction);

//        leaveAction.setPic("xxxxx");
//        最难写的 昨天登录的成绩
        leaveAction.setApplier(AuthInterceptor.getCurrentUser());
//     排序需要的更新时间
        leaveAction.setUpdateTime(LocalDateTime.now());
        //        这里的insert 指的是数据库访问层 中增删改查的新增
        leaveActionMapper.insert(leaveAction);
        return leaveAction;

    }

    // 必定有ID
    @PutMapping("/submit")
    public LeaveAction submit(@RequestBody LeaveSubmitDto leaveSubmitDto) {
        String id = leaveSubmitDto.getLeaveId();
        LeaveAction fromDb = leaveActionMapper.selectById(id);
        if (fromDb == null) {
            throw new MissTargetException(id + "未找到");
        }
//      核心：  状态从A状态转为B状态 ，最好判定它处于A状态
        if (!fromDb.getActionState().equals("toSubmit")) {
            throw new BillStateException("只有待提交的状态才能提交");
        }
        fromDb.setActionState("toAudit");

        BeanUtils.copyProperties(leaveSubmitDto, fromDb);
        fromDb.setApplierTime(LocalDateTime.now());
        fromDb.setUpdateTime(LocalDateTime.now());

//            更新一整条数据
        leaveActionMapper.updateById(fromDb);
        return fromDb;
    }

    @PutMapping("/audit")
    public LeaveAction audit(@RequestBody LeaveAuditDto leaveAuditDto) {
        String id = leaveAuditDto.getLeaveId();
        LeaveAction fromDb = leaveActionMapper.selectById(id);
        if (fromDb == null) {
            throw new MissTargetException("id+未找到");
        }
        //      核心：  状态从A状态转为B状态 ，最好判定它处于A状态
        if (!fromDb.getActionState().equals("toAudit")) {
            throw new BillStateException("只能审核待审核状态的数据");
        }
        fromDb.setActionState("audited");

        fromDb.setAuditMsg(leaveAuditDto.getAuditMsg());
        fromDb.setUpdateTime(LocalDateTime.now());

//            更新一整条数据
        leaveActionMapper.updateById(fromDb);
        return fromDb;
    }

//    写两个查询 分页查询 撑起两个页面
//    申请人页面 审核人页面

    @PostMapping("/query4Applier")
    public IPage<LeaveAction> query4Applier(@RequestBody LeaveApplierQC leaveApplierQC) {
        IPage<LeaveAction> page = Page.of(leaveApplierQC.getPageNum(), leaveApplierQC.getPageSize());
        String currentUser = AuthInterceptor.getCurrentUser();
        IPage<LeaveAction> leaveActionIPage = leaveActionMapper.dynamicForApplier(page, leaveApplierQC, currentUser);
        return leaveActionIPage;
    }

    @PostMapping("/query4Auditor")
    public IPage<LeaveAction> query4Auditor(@RequestBody LeaveAuditorQC qc) {
        IPage<LeaveAction> page = Page.of(qc.getPageNum(), qc.getPageSize());
        String currentUser = AuthInterceptor.getCurrentUser();
        IPage<LeaveAction> leaveActionIPage = leaveActionMapper.dynamicForAuditor(page, qc, currentUser);
        return leaveActionIPage;
    }

    //eyJ0eXAiOiJqd3QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3Mjg4NzAxMTAsImV4cCI6MTcyODg3MTkxMCwic3ViIjoiYXV0aCIsIlVJRCI6InpoYW5nc2FuMDAxIn0.R47ylN50RQMY8qezbFQRngqOyOuueon1lSBZ7B7apdE
//  申请人   未提交前的 小修改
//    leaveType	startTime	endTime	reason	pic
    @PutMapping("/edit")

    public LeaveAction edit(@RequestBody LeaveEditDto leaveEditDto) {
        LeaveAction leaveAction = leaveActionMapper.selectById(leaveEditDto.getLeaveId());
        if (leaveAction == null) {
            throw new MissTargetException("数据已经不存在");
        }
        BeanUtils.copyProperties(leaveEditDto, leaveAction);
//        System.out.println(leaveAction);
        if (!leaveAction.getActionState().equals("toSubmit")) {
            throw new BillStateException("只有待提交状态的数据才能编辑");
        }
//        业务方面的异常 后端 如何做 以便跟顾客直接就留
//        统一异常处理
        leaveAction.setActionState("toSubmit");
        leaveActionMapper.updateById(leaveAction);
        return leaveAction;
    }
    @GetMapping("/findById/{id}")
    public LeaveAction findById(@PathVariable("id") String id) {
        LeaveAction leaveAction = leaveActionMapper.selectById(id);

        if (leaveAction == null) {
            throw new MissTargetException("数据不存在");
        }
        return leaveAction;
    }
}
